var __assign =
  (this && this.__assign) ||
  function () {
    __assign =
      Object.assign ||
      function (t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
      };
    return __assign.apply(this, arguments);
  };
import React, { useEffect } from 'react';
import { Button, Col } from 'antd';
import StandardFormItem from '../../../components/StandardFormItem';
import { useConfigContext } from '../../contexts/ConfigContext';
export function DroppedItem(props) {
  var _a, _b;
  var index = props.index,
    targetIndex = props.targetIndex,
    schema = props.schema;
  var _c = useConfigContext(),
    itemStore = _c.itemStore,
    setSchemaList = _c.setSchemaList,
    activeSchema = _c.activeSchema,
    forceRender = _c.forceRender,
    setActiveSchemaCurrent = _c.setActiveSchemaCurrent,
    setItemStore = _c.setItemStore;
  var id = schema.id,
    type = schema.type,
    configs = schema.configs;
  console.log('drop', { itemStore: itemStore, type: type });
  var StandardInput =
    ((_a = itemStore[type]) === null || _a === void 0 ? void 0 : _a.StandardInput) || 'input';
  var active = activeSchema.current === schema;
  useEffect(
    function () {
      var _a;
      if (JSON.stringify(itemStore) !== '{}') {
        console.log('drop e', type, itemStore);
        StandardInput =
          (_a = itemStore[type]) === null || _a === void 0 ? void 0 : _a.StandardInput;
      }
    },
    [itemStore]
  );
  var deleteSchema = function (e) {
    var _a;
    e.stopPropagation();
    if (active) {
      setActiveSchemaCurrent(null);
    }
    setSchemaList(function (pre) {
      return pre.filter(function (it) {
        return it.id !== id;
      });
    });
    setItemStore(
      __assign(
        __assign({}, itemStore),
        ((_a = {}), (_a[type] = __assign(__assign({}, itemStore[type]), { hidden: false })), _a)
      )
    );
    forceRender();
  };
  var activateSchema = function (e) {
    e.stopPropagation();
    setActiveSchemaCurrent(schema);
    forceRender();
  };
  var handleDragEnter = function () {
    targetIndex.current = index;
  };
  var handleDragEnd = function () {
    setSchemaList(function (schemaList) {
      var temp = schemaList[targetIndex.current];
      schemaList[targetIndex.current] = schemaList[index];
      schemaList[index] = temp;
      return schemaList;
    });
    forceRender();
  };
  return React.createElement(
    Col,
    { span: ((_b = configs.itemProps) === null || _b === void 0 ? void 0 : _b.layout) || 8 },
    React.createElement(
      'div',
      {
        draggable: true,
        className: 'dropped-item '.concat(active ? 'active-item' : ''),
        onClick: activateSchema,
        onDragEnd: handleDragEnd,
        onDragEnter: handleDragEnter
      },
      React.createElement(
        Button,
        { type: 'text', className: 'delete-butt', onClick: deleteSchema },
        'X'
      ),
      React.createElement(StandardFormItem, { StandardInput: StandardInput, configs: configs })
    )
  );
}
